package main

import (
	"fmt"
	"reflect"
)

/*
  反射修改值
*/

func reflectSetValue(x interface{}) {
	// *x = 200 //invalid operation: cannot indirect x (variable of type

	//类型断言
	/* v, _ := x.(*int) //cannot indirect x (variable of type interface{})
	*x = 200 */

	v := reflect.ValueOf(x)
	// v.Kind() // ptr
	fmt.Println(v.Elem().Kind()) //int64  传入是指针变量 需要使用Elem（）
	if v.Elem().Kind() == reflect.Int64 {
		v.Elem().SetInt(123)
	} else if v.Elem().Kind() == reflect.String {
		v.Elem().SetString("hello golang")
	}
}

func reflectSetValue2(x interface{}) {}

func main() {
	var a int64 = 100
	reflectSetValue(&a) // 正确，通过指针来修改
	fmt.Println(a)      //123

	var b string = "hello world"
	reflectSetValue(&b)
	fmt.Println(b) //ello golang

}
